package com.ruoyi.wvp.filter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.poi.hssf.record.DVALRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ExchangeFunction;
import reactor.core.publisher.Mono;

public class LoggingFilter implements ExchangeFilterFunction {

    private static final Logger log = LoggerFactory.getLogger(LoggingFilter.class);

    private ObjectMapper objectMapper= new ObjectMapper();

    @Override
    public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {
        long startTime = System.currentTimeMillis();
        log.info("Request: {} {}", request.method(), request.url());
        log.info("Headers: {}", request.headers());
        log.info("method: {}", request.method());
        try {
            log.info("body: {}", objectMapper.writeValueAsString(request.body()));
        } catch (JsonProcessingException e) {

        }

        return next.exchange(request)
                .doOnSuccess(response -> {
                    long endTime = System.currentTimeMillis();
                    log.info("Response: {} {}", response.statusCode(), response.headers());
                    log.info("Time taken: {} ms", endTime - startTime);
                });
    }
}